package com.lehuowujie.lehuomarket.base;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lehuowujie.lehuomarket.R;
import com.lehuowujie.lehuomarket.dialog.NetLoadDialog;
import com.lehuowujie.lehuomarket.util.UtilIntent;

/**
 * Created by 王瑞铭 on 2017/1/12.
 */
public abstract class BaseActivity extends AppCompatActivity {
    /**
     * 设置页面布局文件
     */
    public abstract int setContentViewRes();

    /**
     * 初始化控件
     */
    public abstract void initView();

    /**
     * 绑定事件
     */
    public abstract void initEvent();

    /**
     * 返回按键
     */
    private LinearLayout back_layout;

    /**
     * 标题
     */
    private TextView toolBarTitle;

    /**
     * 右侧标题
     */
    private TextView toolbarRight;

    public NetLoadDialog mNetDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setContentViewRes());
        initView();
        initEvent();
    }

    /**
     * 初始化toolbar
     *
     * @param
     */
    public void initToolBar() {
        back_layout = (LinearLayout) findViewById(R.id.back_layout);
        toolBarTitle = (TextView) findViewById(R.id.toolbar_title);
        toolbarRight = (TextView) findViewById(R.id.toolbar_right);
        toolbarRight.setVisibility(View.GONE);
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UtilIntent.finishDIY(BaseActivity.this);
            }
        });
    }

    /**
     * 设置toolbar的返回按键
     */
    public void setBack(boolean isBack) {
        if (isBack)
            back_layout.setVisibility(View.VISIBLE);
        else
            back_layout.setVisibility(View.GONE);
    }

    /**
     * 设置toolbar的标题
     *
     * @param resId
     */
    public void setTitle(int resId) {
        toolBarTitle.setText(getString(resId));
    }

    /**
     * 设置toolbar的标题
     *
     * @param title
     */
    public void setTitle(String title) {
        toolBarTitle.setText(title);
    }

    /**
     * 设置toolbar的右侧标题
     *
     * @param resId
     */
    public void setToolBarRightTitle(int resId) {
        toolbarRight.setVisibility(View.VISIBLE);
        toolbarRight.setText(getString(resId));
    }

    /**
     * 设置toolbar的右侧标题
     *
     * @param rightTitle
     */
    public void setToolBarRightTitle(String rightTitle) {
        toolbarRight.setVisibility(View.VISIBLE);
        toolbarRight.setText(rightTitle);
    }

    /**
     * 显示正在加载的对话框
     */
    public void showNetDialog() {
        if (mNetDialog == null)
            mNetDialog = new NetLoadDialog(this, R.style.MyDialogStyle);
        mNetDialog.show();
    }

    /**
     * 隐藏正在加载的对话框
     */
    public void hideNetDialog() {
        if (mNetDialog != null)
            mNetDialog.dismiss();
    }

}
